package com.example.test_witns;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.test_witns.databinding.ActivityUserIntroductionBinding;
import com.example.test_witns.ui.add_friend_requset.ActivityAddFriendRequest;

import java.util.Objects;

import de.hdodenhof.circleimageview.CircleImageView;

public class UserIntroductionActivity extends AppCompatActivity {
    ActivityUserIntroductionBinding binding;

    private String nickname;
    private String userId;
    private String uuid;
    private byte[] userAvatarBytes;
    private String userAvatarURL;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityUserIntroductionBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Objects.requireNonNull(getSupportActionBar()).hide();
        Window window = getWindow();
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        window.setStatusBarColor(Color.parseColor("#dddddd"));

        Intent intent = getIntent();
        userId = intent.getStringExtra("userEmail");
        nickname = intent.getStringExtra("nickname");
        uuid = intent.getStringExtra("uuid");
        userAvatarBytes = intent.getByteArrayExtra("userAvatarBytes");
        userAvatarURL = intent.getStringExtra("userAvatarURL");

        TextView user_introduction_uuid = findViewById(R.id.user_introduction_uuid);
        TextView user_introduction_username = findViewById(R.id.user_introduction_username);
        user_introduction_uuid.setText(uuid);
        user_introduction_username.setText(nickname);

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

        Objects.requireNonNull(getSupportActionBar()).hide();

        View user_introduction_back = findViewById(R.id.navigation_user_introduction_back);
        user_introduction_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        View send_add_friend_request = findViewById(R.id.send_add_friend_request);
        send_add_friend_request.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), ActivityAddFriendRequest.class);
                intent.putExtra("nickname", nickname);
                intent.putExtra("uuid",uuid);
                intent.putExtra("userEmail", userId);
                intent.putExtra("userAvatarBytes", userAvatarBytes);
                startActivity(intent);
            }
        });

        CircleImageView userAvatar = findViewById(R.id.userAvatar);
        Bitmap bitmap = null;
        if (userAvatarBytes != null){
            bitmap = BitmapFactory.decodeByteArray(userAvatarBytes, 0, userAvatarBytes.length);
            userAvatar.setImageBitmap(bitmap);
        }else {
            Glide.with(getApplicationContext()).load(userAvatarURL).into(userAvatar);
        }


    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        outState.putString("username", nickname);
        outState.putString("uuid", uuid);
        outState.putString("userId", userId);
        outState.putByteArray("userAvatar", userAvatarBytes);
    }
}
